<template>
	<div class="app-container">
		<div class="table-wrapper">
			<el-form :inline="true" ref="formRef" :rules="rules" label-position="right" :disabled="route.query.isView" :model="params" label-width="200px">
				<div class="form-inline">
					<el-row justify="center">
						<el-col :span="11">
							<el-form-item label="编号：" class="form-item" prop="number">
								<el-input v-model="params.number" placeholder="" :disabled="true" clearable />
							</el-form-item>
						</el-col>
						<el-col :span="11">
							<el-form-item label="用途：" prop="purpose">
								<el-col :span="12">
									<el-form-item prop="purpose">
										<el-select v-model="params.purpose" placeholder="请选择" class="form-select"
											@change="useChange" clearable>
											<el-option v-for="item in useOptions" :key="item.value" :label="item.label"
												:value="item.value">
											</el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :span="1" class="text-center">
									<span class="text-gray-500">-</span>
								</el-col>
								<el-col :span="11">
									<el-form-item prop="purpose1">
										<el-select v-model="params.purpose1" placeholder="请选择" class="form-select"
											clearable>
											<el-option v-for="item in use1Options" :key="item.value" :label="item.label"
												:value="item.value">
											</el-option>
										</el-select>
									</el-form-item>
								</el-col>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row justify="center">
						<el-col :span="11">
							<!-- <el-form-item label="结构类型：" class="form-item" prop="structureType">
								<el-form-item prop="structureType">
									<el-select v-model="params.structureType" placeholder="请选择" class="form-select" :disabled="!isAdd" clearable>
										<el-option v-for="item in structureTypeOptions" :key="item.value"
											:label="item.label" :value="item.value">
										</el-option>
									</el-select>
								</el-form-item>
							</el-form-item> -->
							<el-form-item label="结构类型：" prop="structureType">
								<el-col :span="11">
									<el-form-item prop="structureType">
										<el-select v-model="params.structureType" placeholder="请选择" class="form-select"
											@change="structureTypeChange" clearable>
											<el-option v-for="item in structureTypeOptions" :key="item.value"
												:label="item.label" :value="item.value"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :span="1" class="text-center"><span class="text-gray-500">-</span></el-col>
								<el-col :span="11">
									<el-form-item prop="structureType1">
										<el-select v-model="params.structureType1" placeholder="请选择" class="form-select"
											clearable>
											<el-option v-for="item in structureType1Options" :key="item.value"
												:label="item.label" :value="item.value"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
							</el-form-item>
						</el-col>
						<el-col :span="11">
							<el-form-item label="区域：" class="form-item" prop="province">
								<el-form-item prop="province" style="width:120px;margin-right:10px;">
									<el-select v-model="params.province" placeholder="请选择省" @change="chooseProvince"
										:disabled="!isAdd" clearable value-key="id">
										<el-option v-for="item in provinceData" :key="item.id" :label="item.name"
											:value="item">
										</el-option>
									</el-select>
								</el-form-item>
								<el-form-item prop="city" style="width:120px;margin-right:10px;">
									<el-select v-model="params.city" placeholder="请选择市" @change="chooseCity"
										:disabled="!isAdd" clearable value-key="id">
										<el-option v-for="item in cityData" :key="item.id" :label="item.name" :value="item">
										</el-option>
									</el-select>
								</el-form-item>
								<el-form-item prop="county" style="width:120px;">
									<el-select v-model="params.county" placeholder="请选择县" @change="chooseCounty"
										:disabled="!isAdd" clearable value-key="id">
										<el-option v-for="item in areaData" :key="item.id" :label="item.name" :value="item">
										</el-option>
									</el-select>
								</el-form-item>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row justify="center">
						<el-col :span="11">
							<el-form-item label="建造年代：" class="form-item" prop="years">
								<el-select v-model="params.years" placeholder="请选择" class="form-select" :disabled="!isAdd"
									clearable>
									<el-option v-for="item in yearsOptions" :key="item.value" :label="item.label"
										:value="item.value">
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="11">
							<el-form-item label="设防情况：" class="form-item" prop="fortification">
								<el-select v-model="params.fortification" placeholder="请选择" class="form-select"
									:disabled="!isAdd" clearable>
									<el-option v-for="item in fortificationOptions" :key="item.value" :label="item.label"
										:value="item.value">
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row justify="center">
						<el-col :span="11">
							<el-form-item label="场地类别：" class="form-item" prop="site">
								<el-input v-model="params.site" placeholder="请输入" clearable />
							</el-form-item>
						</el-col>
						<el-col :span="11">
							<el-form-item label="建筑面积：" class="form-item" prop="buildArea">
								<el-input-number v-model="params.buildArea" :min="0" placeholder="请输入" clearable />
								<span class="color-red">（平方米）</span>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row justify="center">
						<el-col :span="11">
							<el-form-item label="层数：" class="form-item" prop="cs">
								<el-select v-model="params.cs" placeholder="请选择" class="form-select" :disabled="!isAdd"
									clearable>
									<el-option v-for="item in csOptions" :key="item.value" :label="item.label"
										:value="item.value">
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="11">
							<el-form-item label="建筑高度：" class="form-item" prop="buildingHeight">
								<el-input-number v-model="params.buildingHeight" placeholder="请输入" clearable />
								<span class="color-red">（米）</span>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row justify="center">
						<el-col :span="11">
							<el-form-item label="层高：" class="form-item" prop="floorHeight">
								<el-input-number v-model="params.floorHeight" placeholder="请输入" clearable />
								<span class="color-red">（米）</span>
							</el-form-item>
						</el-col>
						<el-col :span="11"></el-col>
						<!-- <el-col :span="11">
							<el-form-item label="地震作用：" class="form-item" prop="earthquakeAction">
								<el-select v-model="params.earthquakeAction" placeholder="请选择" class="form-select"
									clearable>
									<el-option v-for="item in earthquakeActionOptions" :key="item.value" :label="item.label"
										:value="item.value">
									</el-option>
								</el-select>
							</el-form-item>
						</el-col> -->
					</el-row>
					<!-- <el-row justify="center">
						<el-col :span="11">
							<el-form-item label="维护人：" class="form-item" prop="userName">
								<el-input v-model="params.userName" placeholder="" clearable />
							</el-form-item>
						</el-col>
						<el-col :span="11">
							<el-form-item label="维护时间:">
								<el-form-item prop="createTime">
									<el-date-picker clearable v-model="params.createTime" type="date" placeholder="开始时间"
										value-format="YYYY-MM-DD" style="width: 100%" />
								</el-form-item>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row justify="center">
						<el-col :span="22">
							<el-form-item label="维护人所属单位：" class="form-item" prop="maintainerUnit" style="width: 100%;">
								<el-input v-model="params.maintainerUnit" placeholder="" clearable />
							</el-form-item>
						</el-col>
					</el-row> -->
					<el-row justify="center">
						<el-col :span="22">
							<el-form-item label="其他描述：" class="form-item" prop="describe" style="width: 100%;">
								<el-input type="textarea" v-model="params.describe" placeholder="" clearable />
							</el-form-item>
						</el-col>
					</el-row>
					<el-row justify="end" v-if="!route.query.isView">
						<el-form-item class="bottom-button">
							<el-button type="primary" @click="onSave(formRef)"><el-icon><Select /></el-icon>保存</el-button>
							<el-button @click="onRest(formRef)"><el-icon>
									<Refresh />
								</el-icon>重置</el-button>
						</el-form-item>
					</el-row>
				</div>
			</el-form>

			<div class="page-panel">
				<el-form :inline="true" ref="formDamageRef" :disabled="route.query.isView" :model="formDamage" :rules="rulesDamage">
					<el-row>
						<el-col>
							<el-form-item label="6度地震作用弹性最大层间位移角：" prop="txzdcjwyj6">
								<el-input type="number" v-model="formDamage.txzdcjwyj6" placeholder="请输入" maxlength="10">
									<template #suffix>
      								  <i style="color: #333;">%</i>
      								</template>
								</el-input>
							</el-form-item>
							<el-form-item prop="emidAngle6">
								<template #label>
									<Formula tagOffset="-6px" fontSize="20px" :option="[
										{ content: 'α', sub: 's' },
										{ content: '=', margin: '0 8px' },
										{ content: '0.42α', super: '2', sub: 'e' },
										{ content: '+0.59α', sub: 'e' },
										{ content: '+0.115' },
										{ content: '=' },
									]"></Formula>
								</template>
								<el-input type="number" v-model="formDamage.emidAngle6" placeholder="计算结果">
									<template #suffix>
      								  <i style="color: #333;">%</i>
      								</template>
								</el-input>
							</el-form-item>
						</el-col>
						<el-col>
							<el-form-item label="7度地震作用弹性最大层间位移角：" prop="txzdcjwyj7">
								<el-input type="number" v-model="formDamage.txzdcjwyj7" placeholder="请输入" maxlength="10">
									<template #suffix>
      								  <i style="color: #333;">%</i>
      								</template>
								</el-input>
							</el-form-item>
							<el-form-item prop="emidAngle7">
								<template #label>
									<Formula tagOffset="-6px" fontSize="20px" :option="[
										{ content: 'α', sub: 's' },
										{ content: '=', margin: '0 8px' },
										{ content: '0.42α', super: '2', sub: 'e' },
										{ content: '+0.59α', sub: 'e' },
										{ content: '+0.115' },
										{ content: '=' },
									]"></Formula>
								</template>
								<el-input type="number" v-model="formDamage.emidAngle7" placeholder="计算结果">
									<template #suffix>
      								  <i style="color: #333;">%</i>
      								</template>
								</el-input>
							</el-form-item>	
						</el-col>
						<el-col>
							<el-form-item label="8度地震作用弹性最大层间位移角：" prop="txzdcjwyj8">
								<el-input type="number" v-model="formDamage.txzdcjwyj8" placeholder="请输入" maxlength="10">
									<template #suffix>
      								  <i style="color: #333;">%</i>
      								</template>
								</el-input>
							</el-form-item>
							<el-form-item prop="emidAngle8">
								<template #label>
									<Formula tagOffset="-6px" fontSize="20px" :option="[
										{ content: 'α', sub: 's' },
										{ content: '=', margin: '0 8px' },
										{ content: '0.42α', super: '2', sub: 'e' },
										{ content: '+0.59α', sub: 'e' },
										{ content: '+0.115' },
										{ content: '=' },
									]"></Formula>
								</template>
								<el-input type="number" v-model="formDamage.emidAngle8" placeholder="计算结果">
									<template #suffix>
      								  <i style="color: #333;">%</i>
      								</template>
								</el-input>
							</el-form-item>
						</el-col>
						<el-col>
							<el-form-item label="9度地震作用弹性最大层间位移角：" prop="txzdcjwyj9">
								<el-input type="number" v-model="formDamage.txzdcjwyj9" placeholder="请输入" maxlength="10">
									<template #suffix>
      								  <i style="color: #333;">%</i>
      								</template>
								</el-input>
							</el-form-item>
							<el-form-item prop="emidAngle9">
								<template #label>
									<Formula tagOffset="-6px" fontSize="20px" :option="[
										{ content: 'α', sub: 's' },
										{ content: '=', margin: '0 8px' },
										{ content: '0.42α', super: '2', sub: 'e' },
										{ content: '+0.59α', sub: 'e' },
										{ content: '+0.115' },
										{ content: '=' },
									]"></Formula>
								</template>
								<el-input type="number" v-model="formDamage.emidAngle9" placeholder="计算结果">
									<template #suffix>
      								  <i style="color: #333;">%</i>
      								</template>
								</el-input>
							</el-form-item>
						</el-col>
						<el-col>
							<el-form-item label="10度地震作用弹性最大层间位移角：" prop="txzdcjwyj10">
								<el-input type="number" v-model="formDamage.txzdcjwyj10" placeholder="请输入" maxlength="10">
									<template #suffix>
      								  <i style="color: #333;">%</i>
      								</template>
								</el-input>
							</el-form-item>
							<el-form-item prop="emidAngle10">
								<template #label>
									<Formula tagOffset="-6px" fontSize="20px" :option="[
										{ content: 'α', sub: 's' },
										{ content: '=', margin: '0 8px' },
										{ content: '0.42α', super: '2', sub: 'e' },
										{ content: '+0.59α', sub: 'e' },
										{ content: '+0.115' },
										{ content: '=' },
									]"></Formula>
								</template>
								<el-input type="number" v-model="formDamage.emidAngle10" placeholder="计算结果">
									<template #suffix>
      								  <i style="color: #333;">%</i>
      								</template>
								</el-input>
							</el-form-item>
									<el-form-item v-if="!route.query.isView">
								<el-popover placement="top-start" title="提示" :width="200" trigger="hover"
									content="计算结果不保存，若需要保存，请计算完成后点击页面“保存”按钮进行保存！">
									<template #reference>
										<el-button type="primary" @click="onRecalculate(formDamageRef)"><el-icon>
												<EditPen />
											</el-icon>计算</el-button>
									</template>
								</el-popover>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
				<p>
					根据“高层结构层间位移角限值对应关系表”得出此建筑物的震害程度预测结果为：
					<!-- <span class="color-red">{{ resultMsg }}</span> -->
				</p>
				<!-- <el-table class="listTable" ref="tableRef" border :data="tableData" style="width: 100%"
					:cell-style="{ border: '1px solid #e5ebf7' }"
					:header-cell-style="{ border: '1px solid #e5ebf7', borderLeft: 'none' }">
					<el-table-column prop="type" label="结构类型" header-align="center" align="center"
						width="200"></el-table-column>
					<el-table-column prop="intact" label="完好" header-align="center" align="center"></el-table-column>
					<el-table-column prop="slight" label="轻微破坏" header-align="center" align="center"></el-table-column>
					<el-table-column prop="secondary" label="中等破坏" header-align="center" align="center"></el-table-column>
					<el-table-column prop="serious" label="严重破坏" header-align="center" align="center"></el-table-column>
					<el-table-column prop="destroy" label="毁坏" header-align="center" align="center"></el-table-column>
				</el-table> -->
				<el-table :data="resultMsg" border style="width: 100%;margin-bottom: 20px;" v-if="resultMsg.length">
					<el-table-column prop="forecastResults6" header-align="center" align="center" label="6度" />
					<el-table-column prop="forecastResults7" header-align="center" align="center" label="7度" />
					<el-table-column prop="forecastResults8" header-align="center" align="center" label="8度" />
					<el-table-column prop="forecastResults9" header-align="center" align="center" label="9度" />
					<el-table-column prop="forecastResults10" header-align="center" align="center" label="10度" />
				</el-table>
				<!-- 易损性模型库中“高层结构层间位移角限值对应关系表”数据 -->
				<el-table :data="arrTableData" border style="width: 100%">
					<el-table-column type="index" header-align="center" align="center" label="序号" width="80" />
					<el-table-column prop="structureType" header-align="center" align="center" label="结构类型">
						<template #default="scope">
							<span v-if="!scope.row.isEdit">{{ scope.row.structureType }}</span>
							<el-input v-else v-model="scope.row.structureType" size="small"></el-input>
						</template>
					</el-table-column>
					<template v-if="arrTableColumns && arrTableColumns.length > 0">
						<el-table-column v-for="(item, index) in arrTableColumns" :key="index" :prop="item.filedProp"
							:label="item.filedName" header-align="center" align="center">
							<template #default="scope">
								<span v-if="!scope.row.isEdit">{{ scope.row[item.filedProp] }}</span>
								<el-input v-else v-model="scope.row[item.filedProp]" size="small"></el-input>
							</template>
						</el-table-column>
					</template>
				</el-table>
			</div>
		</div>
	</div>
</template>

<script setup>
import { reactive, toRefs } from "vue";
// 引入store
import useUserStore from '@/store/modules/user';
import useDictStore from '@/store/modules/dict';
// 引入接口
import {
	highriseGetInfoByIdApi,//根据id获取信息
	highriseSaveApi,//保存
	highriseCalculationApi,//计算
	getDataById, //回显
} from "@/api/buildingVulnerAnalysis/buildingVulnerAnalysis";
// 获取高层结构层间位移角限值对应关系表
import { getAllHaManage } from "@/api/vulnerModelManagement/highriseManagement";
// 引入行政区划接口
import { getProvince, getChildArea } from "@/api/system/division";
// 引入公式组件
import Formula from '@/components/Formula';

// 获取当前实例
const { proxy } = getCurrentInstance();
// 获取pinia
const userStore = useUserStore();
const dictStore = useDictStore();
// 全局路由对象
const router = useRouter();
// 当前路由对象
const route = useRoute();

// 是否是新增页，有路由参数是详情页，否则是新增页
let isAdd = computed(() => (route.query.chbpiId ? false : true));
// 用户名
const userName = computed(() => userStore.name || '');
// 所属机构名称
const orgName = computed(() => userStore.orgName || '');
// 表单对象
const formRef = ref();
const formDamageRef = ref();


const checkIsPositive = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入大于等于0的正数'))
  } else {
    const reg = /^\d+(?=\.{0,1}\d+$|$)/
    if (reg.test(value)) {
      callback()
    } else {
      callback(new Error('请输入大于等于0的正数'))
    }
  }
}

const data = reactive({
	// 表单参数
	params: {
		chbpiId: '',
		number: '',
		purpose: '', //用途
		purpose1: '',
		region: '', //区域
		province: '', //省
		city: '', //市
		county: '', //区县
		structureType: '', //结构类型
		structureType1: '',
		fortification: '', //设防情况
		years: '', //建造年代
		site: '', //场地类别
		buildArea: null, //建筑面积
		cs: null, //层数
		earthquakeAction: '', //地震作用
		buildingHeight: null, //建筑高度
		floorHeight: null, //层高
		userName: userName.value, //维护人
		maintainerUnit: orgName.value, //维护人所属单位
		// createTime: proxy.parseTime(new Date()) || '', //维护时间
		describe: '', //其他描述
	},
	// 表单验证规则
	rules: {
		province: [{ required: true, message: '请选择', trigger: 'change' }],
		purpose: [{ required: true, message: '请选择', trigger: 'change' }],
		structureType: [{ required: true, message: '请选择', trigger: 'change' }],
		fortification: [{ required: true, message: '请选择', trigger: 'change' }],
		years: [{ required: true, message: '请选择', trigger: 'change' }],
		site: [{ required: true, message: '请输入场地类别', trigger: 'change' }],
		buildArea: [{ required: true, message: '请输入建筑面积', trigger: 'change' }],
		cs: [{ required: true, message: '请输入层数', trigger: 'change' }],
		buildingHeight: [{ required: true, message: '请输入建筑高度', trigger: 'change' }],
		floorHeight: [{ required: true, message: '请输入层高', trigger: 'change' }],
		earthquakeAction: [{ required: true, message: '请选择', trigger: 'change' }],
	},
	// 设防情况
	fortificationOptions: dictStore.fortificationOptions || [],
	// 年份
	yearsOptions: dictStore.yearsOptions || [],
	// 层数
	csOptions: dictStore.csOptions || [],
	// 结构类型
	structureTypeOptions: dictStore.structureTypeOptions3 || [],
	structureType1Options: [],
	// 地震作用
	earthquakeActionOptions: dictStore.earthquakeActionOptions || [],
	// 用途
	useOptions: dictStore.useOptions || [],
	use1Options: [],

	//表格
	tableData: [
		{ type: '框架结构', intact: '1/400', slight: '1/400~1/233', secondary: '1/233~1/85', serious: '1/85~1/33', destroy: '1/33' },
		{ type: '剪力墙和框-剪结构', intact: '1/500', slight: '1/500~1/238', secondary: '1/238~1/86', serious: '1/86~1/33', destroy: '1/33' }
	],

	// 地震剪力计算条件选择表单
	formDamage: {
		txzdcjwyj6: '', // 弹性最大层间位移角
		correctionCoefficient: '', // 校正系数
		emidAngle6: '', // 计算结果值
		txzdcjwyj7: '',
		emidAngle7: '',
		txzdcjwyj8: '',
		emidAngle8: '',
		txzdcjwyj9: '',
		emidAngle9: '',
		txzdcjwyj10: '',
		emidAngle10: '',
	},
	rulesDamage: {
		txzdcjwyj6: [
			{ required: true, trigger: ['blur','change'],validator:checkIsPositive },
		],
		txzdcjwyj7: [
			{ required: true, trigger: ['blur','change'],validator:checkIsPositive },
		],
		txzdcjwyj8: [
			{ required: true, trigger: ['blur','change'],validator:checkIsPositive },
		],
		txzdcjwyj9: [
			{ required: true, trigger: ['blur','change'],validator:checkIsPositive },
		],
		txzdcjwyj10: [
			{ required: true, trigger: ['blur','change'],validator:checkIsPositive },
		],
	},
	// 计算结果总结
	resultMsg: []
});

const {
	// 表单数据
	params,
	// 表单验证规则
	rules,
	// 设防情况选项
	fortificationOptions,
	// 年份选项
	yearsOptions,
	// 层数
	csOptions,
	// 结构类型选项
	structureTypeOptions,
	structureType1Options,
	// 地震作用
	earthquakeActionOptions,
	// 用途选项
	useOptions,
	use1Options,

	// 表格数据
	tableData,

	// 地震剪力计算条件选择表单
	formDamage,
	rulesDamage,
	resultMsg
} = toRefs(data);


// 行政区划选择
const provinceData = ref([]); //省
const cityData = ref([]); //市
const areaData = ref([]); //区、县
/** 获取省列表 */
function getProvinceData() {
	getProvince().then((response) => {
		provinceData.value = response.data;
	});
}
// 选择省份
function chooseProvince(value) {
	params.value.city = null;
	params.value.county = null;
	params.value.province = value.name;
	cityData.value = [];
	areaData.value = [];
	getChildArea(value.id).then(response => {
		cityData.value = response.data;
	});
}
// 选择城市
function chooseCity(value) {
	params.value.city = value.name;
	params.value.county = null;
	areaData.value = [];
	getChildArea(value.id).then((response) => {
		areaData.value = response.data;
	});
}
// 选择区县
function chooseCounty(value) {
	params.value.county = value.name;
}

// 结构类型选择
const structureTypeChange = val => {
	const structureTypes = structureTypeOptions.value.find(item => item.value === val);
	if (structureTypes) {
		params.value.structureType1 = '';
		structureType1Options.value = structureTypes.children;
	}
};
// 选择用途
const useChange = (val) => {
	const use = useOptions.value.find(item => item.value === val)
	if (use) {
		use1Options.value = use.children
	}
}

// 初始化获取数据
const getData = () => {
	highriseGetInfoByIdApi({
		chbpiId: route.query.chbpiId
	}).then(res => {
		if (res.code === 200 && res.data) {
			// 设置数据回显
			setData(res.data);
		}
	}).catch(err => {
		console.log(err);
	});
};
// 查询数据
const onSearch = () => {
	const query = {
		number: route.query.number,
		dataType: route.query.dataType,
		dataId: route.query.pid,
	};
	getDataById(query).then(res => {
		if (res.code === 200 && res.data) {
			// 设置数据回显
			setData(res.data);
		} else {
			proxy.$modal.msgWarning(message || "获取详情失败！");
		}
	}).catch(err => {
		console.log(err);
	});
};
// 设置数据回显
const setData = (data) => {
	// 表单数据回显
	params.value = data;
	// 计算结果
	formDamage.value = {
		// 弹性最大层间位移角
		txzdcjwyj6: data.txzdcjwyj6,
		txzdcjwyj7: data.txzdcjwyj7,
		txzdcjwyj8: data.txzdcjwyj8,
		txzdcjwyj9: data.txzdcjwyj9,
		txzdcjwyj10: data.txzdcjwyj10,
		// 校正系数
		correctionCoefficient: data.correctionCoefficient || '',
		// 计算结果值
		emidAngle6: data.emidAngle6 || '',
		emidAngle7: data.emidAngle7 || '',
		emidAngle8: data.emidAngle8 || '',
		emidAngle9: data.emidAngle9 || '',
		emidAngle10: data.emidAngle10 || '',
	};
	resultMsg.value = [
	{forecastResults6:data.forecastResults6 || '',forecastResults7:data.forecastResults7 || '',forecastResults8:data.forecastResults8 || '',forecastResults9:data.forecastResults9 || '',forecastResults10:data.forecastResults10 || ''},
	]
	// 选择用途
	useChange(data.purpose);
}

//计算
const onRecalculate = async formEl => {
	if (!formEl) return;
	// 表单验证
	await formEl.validate((valid, fields) => {
		if (valid) {
			// 加载遮罩
			proxy.$modal.loading();
			//计算
			highriseCalculationApi({
				// chbpiIds: params.value.chbpiId || '', // 列表页计算使用
				type: params.value.structureType1,
				txzdcjwyj6: parseFloat(formDamage.value.txzdcjwyj6),
				txzdcjwyj7: parseFloat(formDamage.value.txzdcjwyj7),
				txzdcjwyj8: parseFloat(formDamage.value.txzdcjwyj8),
				txzdcjwyj9: parseFloat(formDamage.value.txzdcjwyj9),
				txzdcjwyj10: parseFloat(formDamage.value.txzdcjwyj10),
			}).then(res => {
				// 关闭遮罩层
				proxy.$modal.closeLoading();
				const { code, data } = res;
				if (code == 200 && data) {
					formDamage.value.emidAngle6 = data.emidAngle6;
					formDamage.value.emidAngle7 = data.emidAngle7;
					formDamage.value.emidAngle8 = data.emidAngle8;
					formDamage.value.emidAngle9 = data.emidAngle9;
					formDamage.value.emidAngle10 = data.emidAngle10;
					formDamage.value.correctionCoefficient = data.correctionCoefficient;
					resultMsg.value = [data];
					proxy.$modal.msgSuccess('计算完成！计算结果未保存，若需保存结果，请计算后点击保存按钮！');
				} else {
					resultMsg.value = [];
					proxy.$modal.msgWarning('计算失败！');
				}
			}).catch(err => {
				// 关闭遮罩层
				proxy.$modal.closeLoading();
				console.log(err);
			});
		} else {
			proxy.$modal.msgWarning('表单验证失败，请检测填写数据！');
		}
	});
};
//保存
const onSave = async formEl => {
	if (!formEl) return;
	const formDamageRefValid = await formDamageRef.value.validate((valid, fields) => { valid, fields });
	// 表单验证
	await formEl.validate((valid, fields) => {
		if (valid&&formDamageRefValid) {
			// 加载遮罩
			proxy.$modal.loading();
			//保存
			highriseSaveApi({
				...params.value,
				// 计算结果
				...formDamage.value,
				...resultMsg.value[0]
			}).then(res => {
				// 关闭遮罩层
				proxy.$modal.closeLoading();
				if (res.code == 200 && res.data) {
					proxy.$modal.msgSuccess('保存成功！');
					close()
				} else {
					proxy.$modal.msgWarning('保存失败！');
				}
			}).catch(err => {
				// 关闭遮罩层
				proxy.$modal.closeLoading();
				console.log(err);
			});
		} else {
			proxy.$modal.msgWarning('表单验证失败，请检测填写数据！');
		}
	});
};
/** 保存成功返回查询页 */
function close() {
  const obj = { path: "/buildingVulnerAnalysis/statisticalAnalysis/highriseBuildingState/highriseInquire" };
  proxy.$tab.closeOpenPage(obj);
};
//重置
const onRest = formEl => {
	// 重置表单，方式一
	formEl.resetFields();

	// 只有一个结构类型，默认选中
	if (structureTypeOptions.value?.length&&!route.query.isView) {
		params.value.structureType = structureTypeOptions.value[0].value || '';
		// 结构类型选择
		structureTypeChange(params.value.structureType);
	}
	// 重置表单，方式二
	// proxy.resetForm('formRef');
};

// 高层结构层间位移角限值对应关系表
const arrTableData = ref([]);
const arrTableColumns = ref([]);
// 获取表格数据
const getGridData = () => {
    const paramsp = { titleName: '高层结构层间位移角限值管理' }
    // 高层结构层间位移角限值管理表查询
    getAllHaManage(paramsp).then((res) => {
		const { code, data } = res;
        if (code == 200 && data) {
            buildTableColum(data.dynamicFieldsList)
            buildTableData(data.haManageList)
        }
    }).catch(err => {
        console.log(err);
    });
}
// 构建高层结构层间位移角限值管理表格的动态列结构
const buildTableColum = (dynamicFieldsList) => {
    arrTableColumns.value = [];
    dynamicFieldsList.forEach(item => {
        const cloumn = {
            filedName: item.info,
            filedProp: 'dynamicField-' + item.cbrsColumn,
            cbrsColumn: item.cbrsColumn,
            cbrsId: item.cbrsId,
            cbrsKey: item.cbrsKey,
            checked: false,
            defaultData: item.defaultData
        }
        arrTableColumns.value.push(cloumn)
    })
}
// 构建高层结构层间位移角限值管理表格的数据
const buildTableData = (haManageList) => {
    arrTableData.value = [];
    //循环遍历之前加上非空判断
    if (haManageList && haManageList.length >= 0) {
        haManageList.forEach((item) => {
            // 判断表格中是否已经有该条件
            const found = arrTableData.value.find(element => element.structureType === item.structureType);
            // 已经存在该条件
            if (found) {
                const findConlum = arrTableColumns.value.find(col => col.cbrsId === item.projectId)
                if (findConlum) {
                    found[findConlum.filedProp] = item.structureValue
                }
                //found['dynamicField-' + item.chamColumn] = item.structureValue
                found.chamId.push(item.chamId)
                found.origin.push(item)
            } else {  // 不存在
                const data = {
                    isEdit: false,
                    structureType: item.structureType,
                    defaultData: item.defaultData,
                    chamId: [],
                    origin: []
                }
                //data['dynamicField-' + item.chamColumn] = item.structureValue
                const findConlum = arrTableColumns.value.find(col => col.cbrsId === item.projectId)
                if (findConlum) {
                    data[findConlum.filedProp] = item.structureValue
                }
                data.chamId.push(item.chamId)
                data.origin.push(item)
                arrTableData.value.push(data);
            }
        });
    }
}

// 挂载
onMounted(() => {
	// 只有一个结构类型，默认选中
	if (structureTypeOptions.value?.length) {
		params.value.structureType = structureTypeOptions.value[0].value || '';
		// 结构类型选择
		structureTypeChange(params.value.structureType);
	}
	// 根据参数判断获取数据的方式
	const isLook = route.query.isLook
	if (isLook) {
		// 初始化获取数据，回显
		onSearch();
	} else {
		// 编辑页
		if (!isAdd.value) {
			// 初始化获取数据
			getData();
		}
	}
	// 获取行政区划数据
	getProvinceData();
	// 获取高层结构层间位移角限值对应关系表数据
	getGridData();
});
</script>

<style lang="scss" scoped>
.table-wrapper {
	position: relative;

	h1,
	h2,
	h3,
	h4,
	h5,
	h6 {
		font-weight: bold;
	}

	h4 {
		font-size: 16px;
		color: #107ce5;
		text-align: center;
	}
}

.page-panel {
	margin-top: 40px;
}

.form-inline {
	.el-form-item {
		margin-right: 0;
	}
}

.listTable {
	margin-top: 20px;

	// 表格格子内边距
	// th.el-table__cell>.cell {
	// 	padding: 8px 12px;
	// }
}

// 字体红色
.color-red {
	color: red !important;
}
</style>